#include <stdio.h>
#include <stdlib.h>
#include "list.h"

struct My_DATA {
    list_t list;
    int Data_1;
    int Data_2;
    int Data_3;
};

struct My_DATA *add_Node_tail(list_t *head){
    struct My_DATA *entry;
    entry=(struct My_DATA*)malloc(sizeof(struct My_DATA));
    list_add_tail( &entry->list,head);
    return entry;
}

struct My_DATA *add_Node(list_t *head){
    struct My_DATA *entry;
    entry=(struct My_DATA*)malloc(sizeof(struct My_DATA));
    list_add( &entry->list,head);
    return entry;
}

void remove_Node(struct My_DATA *entry){
    printf("\nremove: Data_1=%d (list %p, next %p, prev %p)\n",
            entry->Data_1,
            &entry->list,
            entry->list.next,
            entry->list.prev);

    list_del(&entry->list);
    free(entry); 
}

void free_List(list_t *head) {
    list_t *listPtr = head->next;
    struct My_DATA *entry;
    
    while(listPtr != head) {
        entry = (struct My_DATA *)listPtr;
        remove_Node(entry);
        listPtr = head->next;
    }
}

void print_List(list_t *head){
    list_t *listPtr;
    struct My_DATA *entry;
 
    for(listPtr = head->next; listPtr!=head; listPtr=listPtr->next) {
        entry = (struct My_DATA *) listPtr;
        printf("entry->Data_1= %d | list addr = %p | next = %p | prev = %p\n",
                entry->Data_1, 
                &entry->list, 
                entry->list.next,  
                entry->list.prev  );
    }
}


int main() {
    list_t *head = (list_t *)malloc(sizeof(list_t));

    initList(head);
    struct My_DATA *itemPtr_1 = add_Node(head);
    itemPtr_1->Data_1 = 100 ;
    struct My_DATA *itemPtr_2 = add_Node(head);
    itemPtr_2->Data_1 = 200 ;
    struct My_DATA *itemPtr_3 = add_Node(head);
    itemPtr_3->Data_1 = 300 ;
    struct My_DATA *itemPtr_4 = add_Node(head);
    itemPtr_4->Data_1 = 400 ;

    print_List(head);

    remove_Node(itemPtr_3);
    print_List(head);
    free_List(head);


    itemPtr_1 = add_Node_tail(head);
    itemPtr_1->Data_1 = 100 ;
    itemPtr_2 = add_Node_tail(head);
    itemPtr_2->Data_1 = 200 ;
    itemPtr_3 = add_Node_tail(head);
    itemPtr_3->Data_1 = 300 ;
    itemPtr_4 = add_Node_tail(head);
    itemPtr_4->Data_1 = 400 ;

    print_List(head);

}

